package com.rzn.reading;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.rzn.reading.dto.Pageable;
import com.rzn.reading.utils.HttpStatus;



@JsonInclude(JsonInclude.Include.NON_NULL)
public class JsonResult<T> implements Pageable {

    private T data;
    private Integer code;
    private Object message;
    private Integer page;
    private Integer pageSize;
    private Integer totalCount;


    public JsonResult() {
    }

    public JsonResult(Object message) {
        this.message = message;
    }


    public JsonResult(T data, Object message) {
        this.data = data;
        this.message = message;
    }

    public JsonResult(T data, Integer code, Object message) {
        this.data = data;
        this.code = code;
        this.message = message;
    }

    public JsonResult(T data, Object message, Pageable pageInfo, Integer totalCount) {
        this(data, message);
        this.page = pageInfo.getPage();
        this.pageSize = pageInfo.getPageSize();
        this.totalCount = totalCount;
    }

    public static JsonResult success(Object data, String message) {
        return new JsonResult(data, HttpStatus.SUCCESS, message);
    }

    public static JsonResult success(Object data) {
        return JsonResult.success(data, "操作成功");
    }

    public static JsonResult success(String message) {
        return JsonResult.success(null, message);
    }

    public static JsonResult success() {
        return JsonResult.success("操作成功");
    }

    public static JsonResult success(Object data, Object message, Pageable pageable, Integer totalCount) {
        JsonResult<Object> result = new JsonResult<>(data, message, pageable, totalCount);
        result.setCode(HttpStatus.SUCCESS);
        return result;
    }

    public static JsonResult success(Object data, Pageable pageable, Integer totalCount) {
        return success(data, "查询成功", pageable, totalCount);
    }

    public static JsonResult error(Object data, String message) {
        return new JsonResult(data, HttpStatus.ERROR, message);
    }

    public static JsonResult error(int code, String message) {
        return new JsonResult(null, code, message);
    }

    public static JsonResult error(String message) {
        return JsonResult.error(null, message);
    }

    public static JsonResult error() {
        return JsonResult.error("操作失败");
    }

    public T getData() {
        return data;
    }

    public JsonResult setData(T data) {
        this.data = data;
        return this;
    }

    public Object getMessage() {
        return message;
    }

    @Override
    public Integer getPage() {
        return page;
    }

    public void setPage(Integer page) {
        this.page = page;
    }

    @Override
    public Integer getPageSize() {
        return pageSize;
    }

    public void setPageSize(Integer pageSize) {
        this.pageSize = pageSize;
    }

    public Integer getTotalCount() {
        return totalCount;
    }

    public void setTotalCount(Integer totalCount) {
        this.totalCount = totalCount;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public void setMessage(Message message) {
        this.message = message.toString();
    }

    public void setMessage(Object message) {
        this.message = message;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }
}
